package test1;

import java.util.*;

public class test {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        int M = scanner.nextInt();
        scanner.nextLine(); // 读取换行符

        while (M-- > 0) {
            StringTokenizer tokenizer = new StringTokenizer(scanner.nextLine());
            int L = Integer.parseInt(tokenizer.nextToken());
            int R = Integer.parseInt(tokenizer.nextToken());
            int X = Integer.parseInt(tokenizer.nextToken());
            int Y = Integer.parseInt(tokenizer.nextToken());
            int num = 0;
            for (int N = 0; N < (1 << 30); N++) {
                int cnt = Integer.bitCount(N); // 统计N的二进制表示中1的个数
                if (cnt >= L && cnt <= R && (N & X) == X && (N | Y) == Y) {
                    num++;
                }
            }
            System.out.println(num);
        }
    }
}

